home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / ici / ici.cpi / catch.c < prev    next >
C/C++ Source or Header  |  1994-10-27  |  2KB  |  99 lines

  1. #include "exec.h"
  2. #include "catch.h"
  3. #include "op.h"
  4. #include "func.h"
  5.  
  6. /*
  7.  * Unwind the execution stack until a catcher is found.  Then unwind
  8.  * the scope and operand stacks to the matching depth (but only if it is).
  9.  * Returns the catcher, or NULL if there wasn't one.
  10.  */
  11. catch_t *
  12. unwind()
  13. {
  14.     object_t    **p;
  15.     catch_t    *c;
  16.  
  17.     for (p = x_top - 1; p >= xs->a_base; --p)
  18.     {
  19.     if (iscatch(*p))
  20.     {
  21.         c = catchof(*p);
  22.         x_top = p;
  23.         o_top = &os->a_base[c->c_odepth];
  24.         v_top = &vs->a_base[c->c_vdepth];
  25.         NEXT_VSVER;
  26.         return c;
  27.     }
  28.     }
  29.     return NULL;
  30. }
  31.  
  32. STATIC long
  33. mark_catch(c)
  34. catch_t    *c;
  35. {
  36.     long    mem;
  37.  
  38.     objof(c)->o_flags |= O_MARK;
  39.     mem = sizeof(catch_t);
  40.     if (c->c_catcher != NULL)
  41.     mem += mark(c->c_catcher);
  42.     return mem;
  43. }
  44.  
  45. /*
  46.  * Note: catch's come back pre-loosed.
  47.  */
  48. catch_t *
  49. new_catch(o, odepth, vdepth)
  50. object_t    *o;
  51. int        odepth;
  52. int        vdepth;
  53. {
  54.     register catch_t    *c;
  55.  
  56.     if ((c = talloc(catch_t)) == NULL)
  57.     return NULL;
  58.     objof(c)->o_type = &catch_type;
  59.     objof(c)->o_tcode = TC_CATCH;
  60.     objof(c)->o_flags = 0;
  61.     objof(c)->o_nrefs = 0;/* Catch's are pre-loosened.  Unlike other types. */
  62.     rego(c);
  63.     c->c_catcher = o;
  64.     c->c_odepth = odepth;
  65.     c->c_vdepth = vdepth;
  66.     return c;
  67. }
  68.  
  69. /*
  70.  * runner handler    => catcher (os)
  71.  *            => catcher pc (xs)
  72.  *            => catcher (vs)
  73.  */
  74. STATIC int
  75. op_onerror()
  76. {
  77.     if ((x_top[-1] = objof(new_catch(o_top[-1], o_top - os->a_base - 2, v_top - vs->a_base))) == NULL)
  78.     return 1;
  79.     if ((*x_top = objof(new_pc(arrayof(o_top[-2])))) == NULL)
  80.     return 1;
  81.     o_top -= 2;
  82.     ++x_top;
  83.     return 0;
  84. }
  85.  
  86. type_t    catch_type =
  87. {
  88.     mark_catch,
  89.     free_simple,
  90.     hash_unique,
  91.     cmp_unique,
  92.     copy_simple,
  93.     assign_simple,
  94.     fetch_simple,
  95.     "catch"
  96. };
  97.  
  98. op_t    o_onerror    = {OBJ(TC_OP, op_type), op_onerror};
  99.